package com.ruoyi.project.data.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.data.domain.MainIndex;
import com.ruoyi.project.data.mapper.MainIndexMapper;
import com.ruoyi.project.data.service.MainIndexService;
import com.ruoyi.project.system.domain.SysConfig;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.mapper.SysConfigMapper;
import com.ruoyi.project.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MainIndexServiceImpl implements MainIndexService {

    @Autowired
    private MainIndexMapper mainIndexMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public MainIndex selectTopData(Map<String, Object> param) {

        MainIndex  result=new MainIndex();

        /**
         * 日运单量统计
         */
        result.setDayYdNum(mainIndexMapper.selectDayWaybill(param));
        /**
         * 年运单量统计
         */
        result.setYearYdNum(mainIndexMapper.selectYearWaybill(param));
        /**
         *  日货物重量
         */
        result.setDayHwWeight(mainIndexMapper.selectDayHwWeight(param));

        /**
         * 年货物重量
         */
        result.setYearHwWeight(mainIndexMapper.selectYearHwWeight(param));


        /**
         * 日超重异常数
         */
//         result.setDayCzycNum(mainIndexMapper.selectDayCzycWeight2(param));


        /**
         * 年超重异常数
         */
//         result.setYearCzycNum(mainIndexMapper.selectYearCzycWeight2(param));

        /**
         * 日车牌异常数
         */
      //  result.setDayCpycNum(mainIndexMapper.selectDayCpycData(param));

        /**
         * 年车牌异常数
         */
     //   result.setYearCpycNum(mainIndexMapper.selectYearCpycData(param));

        //获取运单超时 设置时间
        /**
         * 获取运单异常时限
         */
       /* SysDictData sysDictData= new SysDictData();
        sysDictData.setDictType("waybill_excp_times");
        sysDictData.setDictLabel("waybill_day");
        List<SysDictData> dictList=  dictDataMapper.selectDictDataList(sysDictData);
        Integer  days=Integer.parseInt(dictList.get(0).getDictValue());

        String endTime= DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",DateUtils.addDays(new Date(),-days));
        param.put("out_time",endTime);*/
        /**
         *  日运单异常数
         */
//        result.setDayYdycNum(mainIndexMapper.selectDayYdycData(param));

        /**
         * 年运单异常数
         */
//        result.setYearYdycNum(mainIndexMapper.selectYearYdycData(param));

        /**
         * 日 无称重异常
         */
      //  result.setDayRecordNum(mainIndexMapper.selectDayRecordNum(param));
        /**
         * 年 无称重异常
         */
      //  result.setYearRecordNum(mainIndexMapper.selectYearRecordNum(param));

        result.setTimeOutForkliftNum(mainIndexMapper.selectTimeOutForklift(param));
        result.setForkliftNum(mainIndexMapper.selectForklift(param));

        result.setTimeOutForkliftWorkerNum(mainIndexMapper.selectTimeOutForkliftWorker(param));
        result.setForkliftWorkerNum(mainIndexMapper.selectForkliftWorker(param));

        /**
         * 超载出场 --- 日
         */
//        result.setDayCzccNum(mainIndexMapper.selectDayCzccWeight(param));

        /**
         * 超载出场 --- 累计
         */
//        result.setYearCzccNum(mainIndexMapper.selectCzccWeight(param));

        /**
         * 无称重记录出场----日
         */
//        result.setDayWczccNum(mainIndexMapper.selectDaywczccWeight(param));

        /**
         * 无称重记录出场 --- 累计
         */
//        result.setAllWczccNum(mainIndexMapper.selectWczccWeight(param));


        SysConfig config = sysConfigMapper.checkConfigKeyUnique("weight_all_waybill");
        if (config != null){
            param.put("percentage",Double.parseDouble(config.getConfigValue()));
        }else {
            param.put("percentage",0.5);
        }

        /**
         * 24小时无称重记录----日
         */
//        result.setDayWczjlNum(mainIndexMapper.selectDayWczjlWeight(param));

        /**
         * 24小时无称重记录----累计
         */
//        result.setAllWczjlNum(mainIndexMapper.selectWczjlWeight(param));

        /**
         * 获取预报警数
         */
        Map<String, Object> ybjMap = mainIndexMapper.selectWarningRemind(param);
        if(ybjMap != null){
            result.setDayYjNum(ybjMap.get("yjTotalNum")==null?0:Integer.parseInt(ybjMap.get("yjTotalNum").toString()));
            result.setMonthBjNum(ybjMap.get("bjTotalNum")==null?0:Integer.parseInt(ybjMap.get("bjTotalNum").toString()));
        }else{
            result.setDayYjNum(0);
            result.setMonthBjNum(0);
        }

        return result;
    }

    /**
     * 平台每月订单走向
     * @param param
     * @return
     */
    @Override
    public Map<String, Object> getMonthYdData(Map<String, String> param) {
        // 获取当前日期
        String endDate = DateUtils.getDate();
        String startDate = DateUtils.addDay(new Date(),-6);
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        List<Map<String, Object>> res= mainIndexMapper.getMonthYdData(param);
        List<String> dateArray = new ArrayList<>();
        List<Integer> tjArray = new ArrayList<>();
        Date beginDate = new Date();
        for(int i=6; i>=0 ; i--){
            String date = DateUtils.addDay(new Date(),-i);
            if(res != null && res.size() > 0){
                for(Map<String, Object> map : res) {
                    if(date.equals(map.get("days"))){
                        tjArray.add(Integer.parseInt(map.get("dataNun").toString()));
                        res.remove(map);
                        break;
                    }
                }
            }else{
                tjArray.add(0);
            }
            dateArray.add(date.substring(5,date.length()));
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("dateArray", dateArray);
        resultMap.put("tjArray", tjArray);
        return resultMap;
    }

    /**
     * 获取 月异常情况
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> selectMonthYdycdata(Map<String, String> param) {
       /* List<Map<String, Object>> res=new ArrayList<>();
        Map<String, Object> map=new HashMap<>();
        map.put("months","01月");
        map.put("czycnum",200);
        map.put("cpycnum",300);
        map.put("ydycnum",400);
        res.add(map);

        Map<String, Object> map2=new HashMap<>();
        map2.put("months","02月");
        map2.put("czycnum",230);
        map2.put("cpycnum",330);
        map2.put("ydycnum",430);
        res.add(map2);*/


        List<Map<String, Object>> res= mainIndexMapper.selectMonthYdycdata(param);
        return res;
    }

    /**
     * 查询场站排行
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> selectStationSortData(Map<String, String> param) {
        return  mainIndexMapper.selectStationSortData(param);
    }


}
